package com.alliky.user.ui.fragment

import android.widget.ImageView
import androidx.recyclerview.widget.GridLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.alliky.common.entity.MessageEvent
import com.alliky.common.entity.vo.ServiceVo
import com.alliky.common.global.Global
import com.alliky.common.router.Router
import com.alliky.common.utils.clickWithTrigger
import com.alliky.core.base.BaseFragment
import com.alliky.core.manage.UserManage
import com.alliky.core.utils.ImageLoader
import com.alliky.home.mvp.contract.UserContract
import com.alliky.home.mvp.presenter.UserPresenter
import com.alliky.user.R
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import kotlinx.android.synthetic.main.fragment_user.*

@Route(path = Router.User.Fragment.UserFragment)
class UserFragment : BaseFragment<UserContract.View, UserContract.Presenter>(),
    UserContract.View {

    var mAdapter: ServiceAdapter? = null

    override fun onEvent(message: MessageEvent) {
        super.onEvent(message)
        if (message.id == Global.CODE.LOGIN_SUCCESS) {
            bindData()
        }
    }

    override fun createPresenter(): UserContract.Presenter = UserPresenter()

    override fun setLayout(): Any? = R.layout.fragment_user

    override fun initData() {
        bindData()
    }

    override fun initView() {
        super.initView()
        iv_header.clickWithTrigger {
            if (UserManage.instance?.isLogin == true) {
                ARouter.getInstance().build(Router.User.Activity.LoginActivity).navigation()
            }
        }
        tv_nickname.clickWithTrigger {
            if (UserManage.instance?.isLogin == true) {
                ARouter.getInstance().build(Router.User.Activity.LoginActivity).navigation()
            }
        }
        tv_sex.clickWithTrigger {
            if (UserManage.instance?.isLogin == true) {
                ARouter.getInstance().build(Router.User.Activity.LoginActivity).navigation()
            }
        }


        var username = UserManage.instance?.User?.username
        var serviceList = ArrayList<ServiceVo>()

        serviceList.add(ServiceVo(0, "收货地址", R.mipmap.icon_address))
        serviceList.add(ServiceVo(1, "领券中心", R.mipmap.icon_present))
        serviceList.add(ServiceVo(2, "优惠券 ", R.mipmap.icon_coupon))
        serviceList.add(ServiceVo(3, "我的帮助", R.mipmap.icon_helper))
        serviceList.add(ServiceVo(4, "在线客服", R.mipmap.icon_service))
        serviceList.add(ServiceVo(5, "我的积分", R.mipmap.icon_integral))
        serviceList.add(ServiceVo(6, "退换/售后", R.mipmap.icon_tui))
        serviceList.add(ServiceVo(7, "分销中心", R.mipmap.icon_dist))
        serviceList.toMutableList()

        bindData()
        mAdapter = ServiceAdapter(serviceList)
        mRecyclerView.apply {
            layoutManager = GridLayoutManager(requireContext(), 4)
            adapter = mAdapter
        }
        mAdapter!!.setOnItemClickListener { adapter, view, position ->
            var serviceVo = adapter.data[position] as ServiceVo
            when (serviceVo.id) {
                0 -> {
                    showDefaultMsg(serviceVo.title ?: "")
                }
                1 -> {
                    showDefaultMsg(serviceVo.title ?: "")
                }
                2 -> {
                    showDefaultMsg(serviceVo.title ?: "")
                }
                3 -> {
                    showDefaultMsg(serviceVo.title ?: "")
                }
                4 -> {
                    showDefaultMsg(serviceVo.title ?: "")
                }
                5 -> {
                    showDefaultMsg(serviceVo.title ?: "")
                }
                6 -> {
                    showDefaultMsg(serviceVo.title ?: "")
                }
                7 -> {
                    showDefaultMsg(serviceVo.title ?: "")
                }
            }
        }
    }

    private fun bindData() {
        if (UserManage.instance?.isLogin == true) {
            var user = UserManage.instance?.User
            tv_nickname.text = user?.nickname
            //性别：0.男，1.女 2.保密
            tv_sex.text = when (user?.sex) {
                0 -> "男"
                1 -> "女"
                2 -> "保密"
                else -> ""
            }
            ImageLoader.loadImage(context, user?.avatar, iv_header, R.mipmap.icon_header)
        }
    }

    inner class ServiceAdapter(data: MutableList<ServiceVo>) :
        BaseQuickAdapter<ServiceVo, BaseViewHolder>(
            R.layout.item_service_layout,
            data
        ) {

        override fun convert(holder: BaseViewHolder, item: ServiceVo) {
            holder.setText(R.id.tv_title, item.title)
            var iv = holder.getView(R.id.iv_image) as ImageView
            iv.setImageResource(item.resId)
        }
    }
}